﻿using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;
using FxWinSuper.Annotations;
using FxWinSuper.Util;

namespace FxWinSuper.UI.Controls
{
	/// <summary>
	/// FxContextMenuItem.xaml 的交互逻辑
	/// </summary>
	public partial class FxContextMenuItem : ContentControl, INotifyPropertyChanged
	{
		public FxContextMenuItem()
		{
			InitializeComponent();
		}


		public event PropertyChangedEventHandler PropertyChanged;

		[NotifyPropertyChangedInvocator]
		protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
		{
			PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
		}

		private ImageSource _imageSource;

		public ImageSource Icon
		{
			get => _imageSource;
			set
			{
				_imageSource = value;
				OnPropertyChanged();
			}
		}

		private double _iconWidth = 20;

		public double IconWidth
		{
			get => _iconWidth;
			set
			{
				_iconWidth = value;
				OnPropertyChanged();
			}
		}

		private double _iconHeight = 20;

		public double IconHeight
		{
			get => _iconHeight;
			set
			{
				_iconHeight = value;
				OnPropertyChanged();
			}
		}

		private string _title;

		public string Title
		{
			get => _title;
			set
			{
				_title = value;
				OnPropertyChanged();
			}
		}

		private bool _hasItem;

		public bool HasItem
		{
			get => _hasItem;
			private set
			{
				_hasItem = value;
				OnPropertyChanged();
			}
		}

		public Action Click { get; set; }

		public static CommPropConvert BoolToVisibleConvert => new CommPropConvert(obj => (bool)obj ? Visibility.Visible : Visibility.Collapsed);

		protected override void OnContentChanged(object oldContent, object newContent)
		{
			base.OnContentChanged(oldContent, newContent);
			HasItem = newContent != null;
		}

		private void UiContainer_OnMouseEnter(object sender, MouseEventArgs e)
		{
			Popup popup = Template.FindName("uiPopup", this) as Popup;
			if (popup != null && HasItem)
			{
				popup.IsOpen = true;
			}
		}

		private void UiContainer_OnMouseLeave(object sender, MouseEventArgs e)
		{
			Popup popup = Template.FindName("uiPopup", this) as Popup;
			if (popup != null && HasItem)
			{
				popup.IsOpen = false;
			}
		}

		private void CalcPopupPosition(Popup popup)
		{
			double screenWidth = SystemParameters.PrimaryScreenWidth;
			double screenHeight = SystemParameters.PrimaryScreenHeight;
			FrameworkElement root = popup.Parent as FrameworkElement;
			FrameworkElement child = popup.Child as FrameworkElement;
			Point rootScreentPoint = root.PointToScreen(new Point(0, 0));
			// Console.WriteLine($"{screenWidth}========{screenHeight} |||| {rootScreentPoint.X},{rootScreentPoint.Y} || {root.ActualWidth},{root.ActualHeight}");
			// Console.WriteLine($"{root}, {child}");
			double hx = child.ActualWidth, hy = child.ActualHeight;
			if (rootScreentPoint.X + root.ActualWidth + child.ActualWidth >= screenWidth)
			{
				// 向左展开
				hx = -child.ActualWidth;
			}

			popup.HorizontalOffset = hx;
			popup.VerticalOffset = hy;
		}

		private void uiPopup_Opened(object sender, EventArgs e)
		{
			CalcPopupPosition(sender as Popup);
		}

		private void UiContainer_OnMouseDown(object sender, MouseButtonEventArgs e)
		{
			Click?.Invoke();
		}
	}
}
